/*
 * Copyright (c) csii.com.cn 2016 zhaojin
 */

package com.csii.ssm.service.impl;

import com.csii.ssm.exception.AccessRuntimeException;
import com.csii.ssm.exception.AccessRuntimeExceptionDict;
import com.csii.ssm.mapper.UserMapper;
import com.csii.ssm.model.User;
import com.csii.ssm.service.BaseService;
import com.csii.ssm.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zhaojin on 16-3-23 上午9:53.
 */
@Service("userService")
public class UserServiceImpl extends BaseService<User> implements IUserService {

    private Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public User loginBy(User reqUser) {
        if (null == reqUser) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        if (StringUtils.isEmpty(reqUser.getTel()) && StringUtils.isEmpty(reqUser.getUname()) ) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        User user = userMapper.loginBy(reqUser.getUname(), reqUser.getTel(), reqUser.getPassword());
        if (null == user) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        return user;
    }

    @Override
    public User loadByTel(String tel) {
        if (null == tel) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        if (StringUtils.isEmpty(tel)) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        User user = userMapper.loadByTel(tel);
        if (null == user) {
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_REQUSER_MUSTBY_NOTNULL);
        }
        return user;
    }
}
